package connect

import (
	"litetalk/proto"
	"net"

	"github.com/gorilla/websocket"
	"github.com/sirupsen/logrus"
)

// it's a user connect session
type Channel struct {
	Prev      *Channel
	Next      *Channel
	ch        chan *proto.Msg
	userId    uint
	authToken string
	connTcp   *net.TCPConn
	connWS    *websocket.Conn
}

func NewChannel(size int) *Channel {
	c := new(Channel)
	c.ch = make(chan *proto.Msg, size)
	c.Next = nil
	c.Prev = nil
	return c
}

func (c *Channel) PushMsg(msg *proto.Msg) {
	c.ch <- msg
}

func (c *Channel) Close() {
	if c.connTcp != nil {
		logrus.Info("connect tcp close")
		c.connTcp.Close()
	}
	if c.connWS != nil {
		logrus.Info("connect websocket close")
		c.connWS.Close()
	}
	close(c.ch)
}
